package cn.tedu.boot32.controller;

import cn.tedu.boot32.utils.DBUtils;
import cn.tedu.boot32.vo.Emp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@Controller
public class EmpController {

    @RequestMapping("/insert")
    @ResponseBody
    public String insert(Emp emp) {

        System.out.println("emp = " + emp);

        //获取连接
        try (Connection conn = DBUtils.getConn()) {
            String sql = "insert into myemp values(null,?,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, emp.getName());
            ps.setString(2, emp.getJob());
            ps.setInt(3, emp.getAge());
            ps.setInt(4, emp.getSalary());
            ps.executeUpdate();//执行插入操作
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加完成! <a href='/'>回到首页</a>";
    }

    @RequestMapping("/select")
    @ResponseBody
    public String select() {
        ArrayList<Emp> list = new ArrayList<>();

        try (
                Connection conn = DBUtils.getConn();
        ) {
            String sql = "select * from myemp";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String job = rs.getString(3);
                int age = rs.getInt(4);
                int salary = rs.getInt(5);
                list.add(new Emp(id, name, job, age, salary));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //把员工的信息装进html标签中 最后返回给客户端
        String html = "<table border=1>";
        html += "<caption>员工列表</caption>";
        html += "<tr><th>id</th><th>名字</th><th>工作</th>" +
                "<th>年龄</th><th>工资</th><th>操作</th></tr>";
        for (Emp e : list) {
            html += "<tr>";
            html += "<td>" + e.getId() + "</td>";
            html += "<td>" + e.getName() + "</td>";
            html += "<td>" + e.getJob() + "</td>";
            html += "<td>" + e.getAge() + "</td>";
            html += "<td>" + e.getSalary() + "</td>";
            html += "<td><a href='/delete?id=" + e.getId() + "'>删除</a></td>";
            html += "</tr>";
        }
        html += "</table>";

        return html;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id) {
        System.out.println("id = " + id);

        try (
                Connection conn = DBUtils.getConn();
        ) {

            String sql = "delete from myemp where id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate(); //执行删除SQL语句

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return "删除成功!<a href='/select'>返回列表页面</a>";
    }


    @RequestMapping("/update")
    @ResponseBody
    public String update(Emp emp) {

        System.out.println("emp = " + emp);

        try (
                Connection conn = DBUtils.getConn();
        ) {
            String sql = "update myemp set name=?,job=?,age=?,salary=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, emp.getName());
            ps.setString(2, emp.getJob());
            ps.setInt(3, emp.getAge());
            ps.setInt(4, emp.getSalary());
            ps.setInt(5, emp.getId());
            ps.executeUpdate(); //执行修改操作
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return "修改成功 <a href='/select'>返回列表页面</a>";
    }
}
